var gsAPIURL = "http://ajaxui.nhndesign.com/docs/components/samples/response/api.php";

var gsBID = "NB10597";

namespace("nhn.board", function() {
	function init() {
		nhn.board.showPage("list");
	}
	
	// ȭ����ȯ
	function showPage(sPage, htRequestParam) {
	
		if(sPage == "list") {
			nhn.board.list.show(htRequestParam);
			//nhn.board.view.hide();
			nhn.board.write.hide();
		} else if (sPage =="view") { 
			nhn.board.list.hide();
			//nhn.board.view.show(htRequestParam);
			nhn.board.write.hide();
		} else if (sPage =="write") { 
			nhn.board.list.hide();
			//nhn.board.view.hide();
			nhn.board.write.show(htRequestParam);
		}
	}
	
	function executeAjax(htRequestParam, fCallback) {
		new $Ajax(gsAPIURL, {onload : function(oResponses) {
			var htResponseData = oResponses.json();
			
			if(htResponseData.sResult == "fail") {
				alert("Error");
			}
			
			fCallback(htResponseData.sResult, htResponseData.htReturnValue);
		}, type : "jsonp"}).request(htRequestParam);
	}
	eval(namespace.exports("init", "showPage", "executeAjax"));
});

namespace("nhn.board.list", function() {
	var _htElement = {};

	function show() {
		_attachEvent();
		_setElement();
		_getList();
	}
	
	function hide() {
		$Element(cssquery.getSingle("._list")).hide();		
	}
	function _getList(htRequestParam) {
		if(!htRequestParam) htRequestParam = {};
		htRequestParam["action"] = "list";
		htRequestParam["bid"] = gsBID;
		nhn.board.executeAjax(htRequestParam, _setList);
	}
	
	function _setList(sResult, htReturnValue) {
		//alert("AJAX OK!!");
		if(sResult == "success") {
			var aArticleList;
			
			if(htReturnValue.article_list[0]['num']) {
				alert(htReturnValue.article_list[0]['num']);
				aArticleList = cssquery( "td", _htElement["list_table_articles"][0] );
				
				aArticleList[0].innerHTML = htReturnValue.article_list[0]['num'];

			} else {
			}
		}
		$Element(cssquery.getSingle("[class=_list]")).show();	
	}
	
	function _onClickWrite() {
		nhn.board.showPage("write");
	}
	
	function _setElement() {
		_htElement["list_container"] = cssquery.getSingle("._list");
		_htElement["list_table_tbody"] = cssquery.getSingle("._list_table_tbody", _htElement["list_container"]);
		_htElement["list_table_articles"] = cssquery("tr", _htElement["list_table_body"]);
	}
	function _attachEvent() {
		$Fn(_onClickWrite).attach($Element(cssquery.getSingle("._button_write")), "click");
	}
	eval(namespace.exports("show", "hide"));
});

namespace("nhn.board.write", function() {

	function show() {
		_attachEvent();		
		$Element(cssquery.getSingle("[class=_write]")).show();
	}
	
	function hide() {
		$Element(cssquery.getSingle("._write")).hide();		
	}	
	function _onClickSubmit() {
		var htRequestParam = {};
		htRequestParam["action"] = "insert";
		htRequestParam["bid"] = gsBID;
		htRequestParam["writer"] = $Element(cssquery.getSingle("._write ._writer")).value;
		htRequestParam["subject"] = $Element(cssquery.getSingle("._write ._subject")).value;
		htRequestParam["password"] = $Element(cssquery.getSingle("._write ._password")).value;
		htRequestParam["content"] = $Element(cssquery.getSingle("._write ._content")).value;

		nhn.board.executeAjax(htRequestParam, _onAjax);
	}
	
	function _onAjax(sResult) {
		nhn.board.showPage("list");
	}
	
	function _attachEvent() {
		$Fn(_onClickSubmit).attach($Element(cssquery.getSingle("._button_submit")), "click");
	}	
	eval(namespace.exports("show", "hide"));	
	
});

$Fn(nhn.board.init).attach(window, "load");